package com.jumi.microservice.controller.inner;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jm.member.common.api.web.R;
import com.jumi.microservice.common.base.AjaxResult;
import com.jumi.microservice.dto.reponse.JmUserReponse;
import com.jumi.microservice.dto.reponse.api.RealnameWithdraw;
import com.jumi.microservice.dto.request.JmUserQueryRequest;
import com.jumi.microservice.entity.JmQrcode;
import com.jumi.microservice.entity.JmRealnameAuthentication;
import com.jumi.microservice.entity.JmUser;
import com.jumi.microservice.entity.JmUserInvite;
import com.jumi.microservice.service.*;
import com.jumi.microservice.util.WrapperUtil;
import io.swagger.annotations.Api;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@Api(value = "内部调用--会员", tags = "内部调用--会员")
@DubboService
@RestController
@RequestMapping("/inner/member")
public class InnerJmUserController implements IMemberService {

    @Autowired
    IJmUserService userService;

    @Autowired
    IJmUserInviteService userInviteService;

    @Autowired
    IJmQrcodeService qrcodeService;

    @Override
    @GetMapping("/getMemberByUid")
    public R<JmUserReponse> getMemberByUid(@RequestParam("uid") Integer uid) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("id", uid);
        Page page = new Page(1, 1);
        PageService pageService = (PageService) userService;
        page = pageService.getPage(page, wrapper);
        List<JmUserReponse> records = page.getRecords();
        if (records.size() > 0) {
            JmUserReponse user = records.get(0);
            JmQrcode qrcode = qrcodeService.getQrcodeByUid(uid);

            //当uid查不到数据是返回null,不至于报错
            if (ObjectUtil.isEmpty(qrcode)) {
                user.setCommunityQr(null);
                user.setPersonalQr(null);
                return R.ok(user);
            } else {
                user.setCommunityQr(qrcode.getCommunityQr());
                user.setPersonalQr(qrcode.getPersonalQr());
                return R.ok(user);
            }
        }
        return null;
    }

    @Override
    @GetMapping("/getUidByMobile")
    public R<Integer> getUidByMobile(@RequestParam("mobile")String mobile) {
        JmUser userByMobile = userService.getUserByMobile(mobile);
        if (ObjectUtil.isNotEmpty(userByMobile)) {
            return R.ok(userByMobile.getId());
        }
        return null;
    }

    @Override
    @PostMapping("/getUserByUids")
    public R<List<JmUserReponse>> getMemberByUids(List<Integer> uids) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.in("id", uids);
        wrapper.orderByDesc("id");
        List<JmUser> list = userService.list(wrapper);
        List<JmUserReponse> reponses = new ArrayList<>(list.size());
        for (JmUser jmUser : list) {
            JmUserReponse reponse = new JmUserReponse();
            BeanUtils.copyProperties(jmUser, reponse);
            reponses.add(reponse);
        }
        return R.ok(reponses);
    }

    /**
     * 根据用户id获取邀请用户数量
     *
     * @param uid 用户id
     * @return
     */
    @Override
    @RequestMapping("/getInviteUsers")
    public R getInviteUsers(Long uid) {
        int num = userInviteService.count(
                Wrappers.<JmUserInvite>lambdaQuery()
                        .eq(JmUserInvite::getInviteUid, uid));
        return R.ok(num);
    }

    @Autowired
    IJmRealnameAuthenticationService realNameService;

    @Override
    public R<RealnameWithdraw> getWithdraw(Integer uid) {
        JmUser byId = userService.checkUserExist(uid);
        RealnameWithdraw data = new RealnameWithdraw();
        data.setAvailablePredeposit(byId.getAvailablePredeposit());
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("uid", uid);
        JmRealnameAuthentication realName = realNameService.getOne(wrapper);
        if (ObjectUtil.isNotEmpty(realName)) {
            data.setIdCard(realName.getIdcard());
            data.setAliCode(realName.getAliCode());
            data.setBankCard(realName.getBankCard());
            data.setTruename(realName.getTruename());
        }
        return R.ok(data);
    }

    @Override
    @PostMapping("/getMemberByRequest")
    public R<Page> getMemberByRequest(@RequestParam("request") JmUserQueryRequest request,
                                      @RequestParam("current") Integer current,
                                      @RequestParam("size") Integer size) {
        QueryWrapper wrapper = WrapperUtil.getWrapper(null, request);
        wrapper.orderByDesc("id");
        Page page = userService.getPage(new Page(current, size), wrapper);
        return R.ok(page);
    }

    @Override
    @GetMapping("/updateUserQrByUid")
    public R<Boolean> updateUserQrByUid(String qrCode, Integer uid) {

        JmUser info = userService.getById(uid);
        info.setQrweixinimg(qrCode);

        Boolean bool = userService.updateById(info);

        return R.ok(bool);

    }

}
